//
//  Dictionary+useExt.swift
//  jrw
//
//  Created by cabinzhao on 6/6/17.
//  Copyright © 2017 武汉玖信普惠金融信息服务有限公司. All rights reserved.
//

import Foundation

extension Dictionary {
    /**
     字典转换为JSONString
     
     - parameter dictionary: 字典参数
     
     - returns: JSONString
     */
    var jsonString:String {
        get {
            let nsDict = self as NSDictionary
            return nsDict.jsonString
        }
    }
}

extension NSDictionary {
    /**
     字典转换为JSONString
     
     - parameter dictionary: 字典参数
     
     - returns: JSONString
     */
    var jsonString:String {
        get {
            if (!JSONSerialization.isValidJSONObject(self)) {
                print("无法解析出JSONString")
                return ""
            }
            let data : NSData! = try? JSONSerialization.data(withJSONObject: self, options: []) as NSData!
            let JSONString = NSString(data:data as Data,encoding: String.Encoding.utf8.rawValue)
            return JSONString! as String
        }
    }
}
